package com.prs.crm.action.document;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.document.DraftFormworkType;
import com.prs.crm.service.document.DraftFormworkTypeService;

public class DraftFormWorkTypeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private DraftFormworkType draftFormworkType;
	private DraftFormworkTypeService draftFormworkTypeService;
	private Integer[] ids;
	private PaginationSupport pagination;
	public DraftFormworkType getDraftFormworkType() {
		return draftFormworkType;
	}
	public void setDraftFormworkType(DraftFormworkType draftFormworkType) {
		this.draftFormworkType = draftFormworkType;
	}
	public DraftFormworkTypeService getDraftFormworkTypeService() {
		return draftFormworkTypeService;
	}
	public void setDraftFormworkTypeService(
			DraftFormworkTypeService draftFormworkTypeService) {
		this.draftFormworkTypeService = draftFormworkTypeService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	@Action("listDraftFormworkType")
	public String listDraftFormworkType(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DraftFormworkType.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.draftFormworkTypeService.listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editDraftFormworkType")
	public String editDraftFormworkType(){
		return SUCCESS;
	}
	
	@Action(value="saveDraftFormworkType",results={
			@Result(name="success",type="redirect",location="listDraftFormworkType"),
			@Result(name="error",type="chain",location="editDraftFormworkType")
	})
	public String saveDraftFormworkType(){
		
		if (draftFormworkType.getId()==null) {
			List<DraftFormworkType> dft=this.getDraftFormworkTypeService().getAllDraftFormworkType();
			int f=0;
			for (DraftFormworkType dt : dft) {
				if (draftFormworkType.getName().equals(dt.getName())) {
					f++;
				}
			}
				if (f>0) {
					this.addActionError("模板名称不能重复！");
					return ERROR;
				}
			}
		if (draftFormworkType.getName()==null || "".equals(draftFormworkType.getName().trim())) {
				this.addActionError("模板名称不能为空！");
				return ERROR;
		}
		this.getDraftFormworkTypeService().save(draftFormworkType);
		return SUCCESS;
	}
	
	@Action(value="removeDraftFormworkType",results={
			@Result(name="success",type="redirect",location="listDraftFormworkType"),
			@Result(name="error",type="chain",location="listDraftFormworkType")
	})
	public String removeDraftFormworkType(){
			if (ids==null) {
				this.setPrompt("请选择一行！");
				return ERROR;
			}
			this.getDraftFormworkTypeService().batchRemove(ids);
		return SUCCESS;
	}
}
